package sk.tuke.client.view;

import sk.tuke.client.interfaces.LayoutFactory;

import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.RestDataSource;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.HTMLPane;
import com.smartgwt.client.widgets.form.ValuesManager;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;

public class MedicalRecordView extends VLayout {
	
	private Integer medicalRecordNumber;
	
	final ValuesManager valuesManager = new ValuesManager();
	
	ListGrid noteGrid = new ListGrid();
	ListGrid diseaseGrid = new ListGrid();
	ListGrid drugGrid = new ListGrid();
	ListGrid symptomGrid = new ListGrid();
	
	HTMLPane leftHTMLPane = new HTMLPane();
	HTMLPane rightHTMLPane = new HTMLPane();
	
	public MedicalRecordView(Integer medicalRecordNumber) {
		super();
		this.medicalRecordNumber=medicalRecordNumber;
		initLayout();
	}

	private void initLayout(){

		RestDataSource datasource = new RestDataSource();  
		datasource.setDataURL("http://localhost:8080/healthCard/healthcardmanagement/getmedicalrecord");  
		datasource.setRecordXPath("GetMedicalRecordOutput/medicalRecord");  
		datasource.setCallbackParam("GetMedicalRecordOutput");
		datasource.setDataFormat(DSDataFormat.JSON);
		DataSourceField date = new DataSourceField("date", FieldType.DATE);
		datasource.addField(date);
		
		RestDataSource diseaseDatasource = new RestDataSource();  
        diseaseDatasource.setDataURL("http://localhost:8080/healthCard/healthcardmanagement/getmedicalrecord");  
        diseaseDatasource.setRecordXPath("GetMedicalRecordOutput/medicalRecord/diagnose/disease");  
        diseaseDatasource.setCallbackParam("GetMedicalRecordOutput");
        diseaseDatasource.setDataFormat(DSDataFormat.JSON);
        
        RestDataSource symptomDatasource = new RestDataSource();  
        symptomDatasource.setDataURL("http://localhost:8080/healthCard/healthcardmanagement/getmedicalrecord");  
        symptomDatasource.setRecordXPath("GetMedicalRecordOutput/medicalRecord/diagnose/symptoms");  
        symptomDatasource.setCallbackParam("GetMedicalRecordOutput");
        symptomDatasource.setDataFormat(DSDataFormat.JSON);
        
        RestDataSource drugDatasource = new RestDataSource();  
        drugDatasource.setDataURL("http://localhost:8080/healthCard/healthcardmanagement/getmedicalrecord");  
        drugDatasource.setRecordXPath("GetMedicalRecordOutput/medicalRecord/diagnose/drugs");  
        drugDatasource.setCallbackParam("GetMedicalRecordOutput");
        drugDatasource.setDataFormat(DSDataFormat.JSON);
        
        RestDataSource noteDatasource = new RestDataSource();  
        noteDatasource.setDataURL("http://localhost:8080/healthCard/healthcardmanagement/getmedicalrecord");  
        noteDatasource.setRecordXPath("GetMedicalRecordOutput/medicalRecord/medicalNotes");  
        noteDatasource.setCallbackParam("GetMedicalRecordOutput");
        noteDatasource.setDataFormat(DSDataFormat.JSON);

        valuesManager.setDataSource(datasource);
        
        Criteria criteria = new Criteria("number", String.valueOf(medicalRecordNumber));
        valuesManager.fetchData(criteria, new DSCallback() {
			public void execute(DSResponse response, Object rawData, DSRequest request) {
				String leftHTMLPaneContent = "<span style='font-size: 12px'><b>Medical record " +
		    			valuesManager.getValueAsString("number") + "</b><br/>" +
		    			valuesManager.getValueAsString("date") + "<br/>" +
		    			"Status: " + valuesManager.getValueAsString("status");
		    			

				String rightHTMLPaneContent = "<u>Diagnose</u><br/>" +
		    			"Name: " + valuesManager.getValueAsString("diagnose/name") + "<br/>" +
		    			"Description: " + valuesManager.getValueAsString("diagnose/description");
				
				leftHTMLPane.setContents(leftHTMLPaneContent);
				leftHTMLPane.redraw();
				
				rightHTMLPane.setContents(rightHTMLPaneContent);
				rightHTMLPane.redraw();
			}
		});
        
        
        leftHTMLPane.setOverflow(Overflow.VISIBLE);
        leftHTMLPane.setWidth(350);
        leftHTMLPane.setContents("No data loaded");
        
        rightHTMLPane.setOverflow(Overflow.VISIBLE);
        rightHTMLPane.setWidth(350);
        rightHTMLPane.setContents("No data loaded");
		  
        SectionStack diseaseSectionStack = new SectionStack();  ;  
        diseaseSectionStack.setHeight(200);   
        diseaseSectionStack.setMargin(30);
        SectionStackSection diseaseSection = new SectionStackSection("Diseases");  
        diseaseSection.setCanCollapse(false);  
        diseaseSection.setExpanded(true);  
        diseaseGrid.setHeight(200);
        diseaseGrid.setDataSource(diseaseDatasource); 
        diseaseGrid.setAutoFetchData(true);
        diseaseGrid.setShowAllRecords(true);
        diseaseGrid.fetchData(new Criteria("number", String.valueOf(medicalRecordNumber)));
        ListGridField diseaseName = new ListGridField("name", "Name");
        ListGridField diseaseDescription = new ListGridField("description", "Description");
        diseaseGrid.setFields(diseaseName, diseaseDescription);
        diseaseSection.setItems(diseaseGrid);  
        diseaseSectionStack.setSections(diseaseSection);
        
        SectionStack drugSectionStack = new SectionStack();  ;  
        drugSectionStack.setHeight(200);   
        drugSectionStack.setMargin(30);
        SectionStackSection drugSection = new SectionStackSection("Drugs");  
        drugSection.setCanCollapse(false);  
        drugSection.setExpanded(true);
        drugGrid.setHeight(200);
        drugGrid.setDataSource(drugDatasource); 
        drugGrid.setAutoFetchData(true);
        drugGrid.setShowAllRecords(true);
        drugGrid.fetchData(new Criteria("number", String.valueOf(medicalRecordNumber)));
        ListGridField drugName = new ListGridField("name", "Name");
        ListGridField drugDescription = new ListGridField("description", "Description");
        drugGrid.setFields(drugName, drugDescription);
        drugSection.setItems(drugGrid);  
        drugSectionStack.setSections(drugSection);
        
        SectionStack symptomSectionStack = new SectionStack();  ;  
        symptomSectionStack.setHeight(200);   
        symptomSectionStack.setMargin(30);
        SectionStackSection symptomSection = new SectionStackSection("Symptoms");  
        symptomSection.setCanCollapse(false);  
        symptomSection.setExpanded(true);
        symptomGrid.setHeight(200);
        symptomGrid.setDataSource(symptomDatasource); 
        symptomGrid.setAutoFetchData(true);
        symptomGrid.setShowAllRecords(true);
        symptomGrid.fetchData(new Criteria("number", String.valueOf(medicalRecordNumber)));
        ListGridField symptomName = new ListGridField("name", "Name");
        ListGridField symptomDescription = new ListGridField("description", "Description");
        symptomGrid.setFields(symptomName, symptomDescription);
        symptomSection.setItems(symptomGrid);  
        symptomSectionStack.setSections(symptomSection);
        
        SectionStack noteSectionStack = new SectionStack();  ;  
        noteSectionStack.setHeight(200);   
        noteSectionStack.setMargin(30);
        SectionStackSection noteSection = new SectionStackSection("Medical Notes");  
        noteSection.setCanCollapse(false);  
        noteSection.setExpanded(true);
        noteGrid.setHeight(200);
        noteGrid.setDataSource(noteDatasource); 
        noteGrid.setAutoFetchData(true);
        noteGrid.setShowAllRecords(true);
        noteGrid.fetchData(new Criteria("number", String.valueOf(medicalRecordNumber)));
        ListGridField noteName = new ListGridField("name", "Name");
        ListGridField noteDescription = new ListGridField("description", "Description");
        noteGrid.setFields(noteName, noteDescription);
        noteSection.setItems(noteGrid);  
        noteSectionStack.setSections(noteSection);
        
        HLayout middleLayout = new HLayout();
        middleLayout.addMember(noteSectionStack);
        middleLayout.addMember(symptomSectionStack);
        
        HLayout bottomLayout = new HLayout();
        bottomLayout.addMember(diseaseSectionStack);
        bottomLayout.addMember(drugSectionStack);
        
		HLayout topLayout = new HLayout(10);
		topLayout.setBorder("1px solid lightgray");
		topLayout.setWidth100();
		topLayout.setAutoHeight();
		topLayout.setPadding(10);
		topLayout.setMargin(30);
		topLayout.addMember(leftHTMLPane);
		topLayout.addMember(rightHTMLPane);
			
		this.setMembersMargin(0);
		this.addMember(topLayout);
        this.addMember(middleLayout);
        this.addMember(bottomLayout);
        this.setOverflow(Overflow.AUTO);
		// initialise the Accounts View layout container
		this.addStyleName("crm-ContextArea");
		this.setWidth("*");

	}
	
	

	public static class Factory implements LayoutFactory {

		public Canvas create() {
			return new MedicalRecordView(null);
		}
		
		public Canvas create(Integer medicalRecordNumber) {
			return new MedicalRecordView(medicalRecordNumber);
		}
		
		
	}
}
